package org.languageguide.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class SavedWord {
	
	String word;
	String translation;
	Timestamp  date;
	boolean hasMore;
	Integer id;
	Integer toTranslateId;
	
	public SavedWord(ResultSet rs) throws SQLException{
		toTranslateId = rs.getInt("ToTranslate.Id");
		id = rs.getInt("SavedWords.Id");
		word = rs.getString("ToTranslate.Text");
		String language = rs.getString("SavedWords.Language");
		translation = rs.getString("Translated.Text");
		if (translation == null){
			translation = rs.getString("RealTrans.Text");
			//System.out.println(otherTranslation);
		}
		String target = rs.getString("Translated.TargetLanguage");
		date = rs.getTimestamp("Created");
		hasMore = rs.next();
		if (hasMore){
			//Integer nextId = rs.getInt("ToTranslate.Id");
			Integer nextId = rs.getInt("SavedWords.Id");
			if (id.equals(nextId)){
				//core implementation
				String otherTranslation = rs.getString("Translated.Text");
				//String otherTarget = rs.getString("Translated.TargetLanguage");
				if (language.equals(target)){
					word = translation;
					translation = otherTranslation; 
				}
				else{
					word = otherTranslation;
					//translation = translation;
				}
				hasMore = rs.next();
			}
		}
	}
	
	public boolean hasMore(){
		return hasMore;
	}
	
	public String getTableRow(){
		
		SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy h:mm a");
		String dateStr = formatter.format(date);
		String checkbox = "<input class=\"checkbox\" id=\"c"+ id + "\" value=\""+ id + "\" type=\"checkbox\">";
		return "<tr class=\"row\" id=\"t" + id +  "\" draggable=\"true\"><td align=\"right\">" + checkbox + "</td><td align=\"left\"><strong>" + word + "</strong> <em>" + translation + "</em></td><td nowrap=\"nowrap\">" + dateStr + "</td><!--<td><button onclick=\"javascript:remove(" + id + ")\">Remove</button></td>--></tr>";  
	}	
	

}